package com.example.home.hospital.activity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.example.home.R;
import com.example.home.hospital.adapter.HospitalBannerAdapter;
import com.example.home.hospital.adapter.HospitalListAdapter;
import com.example.home.hospital.bean.HospitalBanner;
import com.example.home.hospital.bean.HospitalListBean;
import com.example.home.tool.OKHttpTool;

import java.util.Timer;
import java.util.TimerTask;

public class HospitalMain extends AppCompatActivity {

    private Timer timer;
    private int currentPage = 0;
    private final long DELAY_MS = 1000;
    private final long PERIOD_MS = 3000;

    private RelativeLayout qeq;
    private ImageView back;
    private SearchView houseSearch;
    private TextView btnSearch;
    private ViewPager vpBanner;
    private HospitalBannerAdapter hospitalBannerAdapter;
    private RecyclerView rcv_list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hospital_main);

        initView();
        //轮播图
        getBanner();
        startAutoScroll();

        getHospital();
    }

    private void getHospital() {
        OKHttpTool.doNetRequest("prod-api/api/hospital/hospital/list",
                OKHttpTool.GET,
                HospitalListBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        HospitalListBean hospitalListBean = (HospitalListBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindList(hospitalListBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                }
        );
    }

    private void bindList(HospitalListBean hospitalListBean) {
        RecyclerView recyclerView = findViewById(R.id.rcv_list);
        HospitalListAdapter hospitalListAdapter = new HospitalListAdapter(HospitalMain.this, hospitalListBean.getRows());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getBaseContext());
        recyclerView.setAdapter(hospitalListAdapter);
        recyclerView.setLayoutManager(linearLayoutManager);
    }

    private void getBanner() {
        OKHttpTool.doNetRequest("prod-api/api/hospital/banner/list",
                OKHttpTool.GET,
                HospitalBanner.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        HospitalBanner hospitalBanner = (HospitalBanner) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindBannershow(hospitalBanner);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                }
        );
    }

    private void bindBannershow(HospitalBanner hospitalBanner) {
        ViewPager viewPager = findViewById(R.id.vp_banner);
        hospitalBannerAdapter = new HospitalBannerAdapter(HospitalMain.this, hospitalBanner.getData());
        viewPager.setAdapter(hospitalBannerAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currentPage = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void startAutoScroll() {
        final Handler handler = new Handler();
        final Runnable update = new Runnable() {
            @Override
            public void run() {
                if (currentPage == hospitalBannerAdapter.getCount()) {
                    currentPage = 0;
                }
                vpBanner.setCurrentItem(currentPage++, true);
                ;
            }
        };
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.post(update);
            }
        }, DELAY_MS, PERIOD_MS);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }
    }

    private void initView() {
        back = findViewById(R.id.back);
        houseSearch = findViewById(R.id.house_search);
        btnSearch = findViewById(R.id.btn_search);
        vpBanner = findViewById(R.id.vp_banner);
        rcv_list = findViewById(R.id.rcv_list);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HospitalMain.this.finish();
            }
        });

    }
}